using System;
using System.Globalization;

/* =====================================================================

  File:      _AccountSettings.ascx.cs for Adventure Works Cycles Storefront Sample
  Summary:   Code behind for an ASP.NET user control for creating or updating customer contact 
			 information.
  Date:	     April 16, 2004

---------------------------------------------------------------------

  This file is part of the Microsoft SQL Server Code Samples.
  Copyright (C) Microsoft Corporation.  All rights reserved.

This source code is intended only as a supplement to Microsoft
Development Tools and/or on-line documentation.  See these other
materials for detailed information regarding Microsoft code samples.

THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.

======================================================= */

namespace Microsoft.Samples.SqlServer {
	/// <summary>
	/// An ASP.NET user control for creating or updating customer contact 
	/// information.
	/// </summary>
    public partial class _AccountSettings_ascx : System.Web.UI.UserControl 
	{
		private bool isNewAccount = true;

		/// <summary>
		/// Indicates whether the control will be used for initial entry
		/// of customer information, or updating for an existing entry.
		/// </summary>
		/// <value>If true then the control is being used to create a new customer account</value>
		public bool IsNewAccount
		{
			get
			{
				return this.isNewAccount;
			}
			set
			{
				this.isNewAccount = value;
			}
		}

		/// <summary>
		/// Retrieves the content from the first name text box.
		/// </summary>
		/// <value>The first name specified</value>
		public String FirstNameText
		{
			get
			{
				return FirstName.Text;
			}
		}

		/// <summary>
		///	Retrieves the content from the last name text box
		/// </summary>
		/// <value>The last name specified</value>
		public String LastNameText
		{
			get
			{
				return LastName.Text;
			}
		}

		/// <summary>
		/// Retrieves the content from the Email text box
		/// </summary>
		/// <value>The email address specified</value>
		public String EmailText
		{
			get
			{
				return Email.Text;
			}
		}

		/// <summary>
		/// Retrieves the content from the old password text box
		/// </summary>
		/// <value>The existing password specified</value>
		public String OldPasswordText
		{
			get
			{
				return OldPassword.Text;
			}
		}

		/// <summary>
		/// Retrieves the content from the password text box
		/// </summary>
		/// <value>The desired password</value>
		public String PasswordText
		{
			get
			{
				return Password.Text;
			}
		}

		public String ConfirmPasswordText
		{
			get
			{
				return ConfirmPassword.Text;
			}
		}

		/// <summary>
		/// Retrieves the state of the email promotion check box
		/// </summary>
		/// <value>If true then a subscription is desired</value>
		public bool IsEmailPromotionChecked
		{
			get
			{
				return emailPromotionCheckBox.Checked;
			}
		}

		/// <summary>
		/// Information which should be displayed to the user
		/// </summary>
		/// <value>The information to display</value>
		public String ErrorMessage
		{
			get
			{
				return MyError.Text;
			}
			set
			{
				MyError.Text = value;
			}
		}

		/// <summary>
		/// The signature of any handler which wishes to be run when users 
		/// create or update their customer information.
		/// </summary>
		/// <param name="sender">Who is initiating the event</param>
		/// <param name="e">Information about which customer is being accessed, and 
		///					the state of the subscription.</param>
		public delegate void AccountSettingsUpdateEventHandler(object sender,
															   AccountSettingsUpdateEventArgs
			e);

		/// <summary>
		/// Where to add your event handler
		/// </summary>
		public event AccountSettingsUpdateEventHandler
			OnAccountSettingsUpdateHandler;

		/// <summary>
		/// Invoke this to raise the event.  This should only be called by code behind from pages which
		/// host this control.
		/// </summary>
		public void OnAccountSettingsUpdate()
		{
			AccountSettingsUpdateEventArgs args = new
				AccountSettingsUpdateEventArgs(
				   new CustomerDetails(this.Context.User.Identity.Name,
									   this.FirstNameText,
									   this.LastNameText,
									   this.EmailText,
									   null, //No need to pass password hash and salt
									   null,
									   (this.IsEmailPromotionChecked) ?
											   1 : 0));

			if (OnAccountSettingsUpdateHandler != null)
				OnAccountSettingsUpdateHandler(this, args);
		}

		/// <summary>
		/// Initialization of the controls when the page is requested by the user.
		/// </summary>
		/// <param name="sender">Who is raising the event</param>
		/// <param name="e">Any extra information about the event</param>
		void Page_Load(object sender, EventArgs e)
		{
			if (isNewAccount)
			{
				//If this is a new account there is no previous password to ask for, but
				//we must insist that the user provide a password and confirmation password,
				//and that they match.
				OldPasswordPanel.Visible = false;
				OldPasswordPanel.Enabled = false;
				PasswordValidator.Enabled = true;
				ConfirmPasswordRequiredValidator.Enabled = true;
				ControlTitle.Text = "Create a New Account";
			}
			else
			{
				//If this is an existing account then we must ask for the old password as an extra 
				//security check before changing user settings, but the user isn't required to specify a
				//new password if they want to keep the existing one.
				OldPasswordPanel.Visible = true;
				OldPasswordPanel.Enabled = true;
				PasswordValidator.Enabled = false;
				ConfirmPasswordRequiredValidator.Enabled = false;
				ControlTitle.Text = "Update Your Account";
				if (!Page.IsPostBack)
				{
					//Load the current customer information into the form
					CustomersDB customers = new CustomersDB();
					CustomerDetails details =
									customers.GetCustomerDetails(Context.
																		   User.
																		   Identity.
																		   Name);

					FirstName.Text = details.FirstName;
					LastName.Text = details.LastName;
					Email.Text = details.Email;
					emailPromotionCheckBox.Checked = (details.EmailPromotion >
													  0);
					Page.Session["PWSalt"] = details.PasswordSalt;
					Page.Session["PWHash"] = details.PasswordHash;
				}
			}
		}
	}

	/// <summary>
	/// A container class used to hold extra information about the AccountSettingsUpdate event
	/// </summary>
	public class AccountSettingsUpdateEventArgs : EventArgs
	{
		 
		/// <summary>
		///		Information about the current account details for the customer being updated
		/// </summary>
		private CustomerDetails customerDetails = null;


		/// <summary>
		/// Constructor for creating an instance of this class 
		/// with the data members initialized.
		/// </summary>
		/// <param name="customerID">Which customer</param>
		/// <param name="emailPromotion">What kind of subscription if any they want</param>
		public AccountSettingsUpdateEventArgs(CustomerDetails customerDetails)
		{
			this.customerDetails = customerDetails;
		}

		public CustomerDetails CustomerDetails
		{
			get
			{
				return this.customerDetails;
			}
		}

	}

}